(function() {
  $(function() {
    var acct_no, diffs, field, form, name, record, _i, _len;
    form = $('form.main');
    form.form({
      inline: true,
      fields: {
        t60_termid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        tz1_termid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        t60_fee: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        p2_fund: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        fee_take_type: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        fund_lack_proc: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        fund_overdraw: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }, {
              type: 'number'
            }
          ]
        },
        pay_chnl: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        pay_merid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        acct_no: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }, {
              type: 'integer[..]'
            }
          ]
        },
        acct_name: {
          optional: true,
          rules: [
            {
              type: 'doesntContain[ ]'
            }
          ]
        },
        acct_mobile: {
          optional: true,
          rules: [
            {
              type: 'integer[..]'
            }
          ]
        }
      }
    });
    diffs = form.form('get value', 'diffs');
    if (diffs && diffs.trim().length > 0) {
      diffs = JSON.parse(diffs);
      form.form('set value', 'diffs', '');
      for (_i = 0, _len = diffs.length; _i < _len; _i++) {
        name = diffs[_i];
        field = form.find("input[name=" + name + "]").parents('.field');
        field.addClass('changed');
      }
    }
    record = form.form('get value', 'record');
    if (record && record.trim().length > 0) {
      record = JSON.parse(record);
      form.form('set value', 'record', '');
    } else {
      record = null;
    }
    form.find('.dropdown.pay_chnl').dropdown({
      onChange: function(value, text, $choice) {
        $(this).addClass('loading');
        return $.post('/p2/pay_merids', {
          chnl: value
        }, function(resp) {
          var items, merid, _j, _len1, _ref;
          form.find('.dropdown.pay_chnl').removeClass('loading');
          if (!resp.succ) {
            return;
          }
          form.find('.dropdown.pay_merid').dropdown('clear');
          items = '';
          _ref = resp.merids;
          for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
            merid = _ref[_j];
            items += "<div class='item' data-value='" + merid.uuid + "'>";
            items += "" + merid.merno;
            items += "<div class='right floated' style='font-size:small; color:#888;'>" + merid.name;
            items += "</div>";
            items += "</div>";
          }
          form.find('.dropdown.pay_merid .menu').html(items);
          form.find('.dropdown.pay_merid').dropdown('refresh');
          if (record && record.pay_merid) {
            return form.form('set value', 'pay_merid', record.pay_merid);
          }
        });
      }
    });
    if (record) {
      form.form('set values', record);
      if (record._checking) {
        acct_no = form.form('get value', 'acct_no');
        if (acct_no.length > 0) {
          form.find('.input.acct_no').removeClass('nofreeze');
        }
        $('body').css('background-color', 'black');
        freeze_form(form);
      }
    }
    return form.submit(function(evt) {
      var acct_name, cred_no, cred_type, result;
      if (!$(this).form('is valid')) {
        return evt.preventDefault();
      }
      cred_type = form.form('get value', 'acct_cred_type');
      cred_no = form.form('get value', 'acct_cred_no');
      if (parseInt(cred_type) === 1 && cred_no.trim().length > 0) {
        result = validate_idno(cred_no, '收款账户开户人');
        if (!result.succ) {
          alert(result.errmsg);
          form.find('button#ok').removeClass('loading');
          return evt.preventDefault();
        }
      }
      acct_no = form.form('get value', 'acct_no');
      if (acct_no.length > 0) {
        if (!validate_cardno(acct_no)) {
          if (!confirm("收款账号可能存在错误，选择［取消］按钮返回检查，或［确定］按钮继续提交.")) {
            form.find('button#ok').removeClass('loading');
            return evt.preventDefault();
          }
        }
      }
      acct_name = form.form('get value', 'acct_name');
      if (cred_no.trim().length === 0 && acct_name.trim().length === 0) {
        alert('账户开户人姓名和证件号码必须至少填写一个');
        form.find('button#ok').removeClass('loading');
        return evt.preventDefault();
      }
    });
  });

}).call(this);
